package com.automannn.codeGenerator.core.startup;

import com.automannn.codeGenerator.core.context.VelocityContext;
import com.automannn.codeGenerator.core.resource.loader.ClasspathResourceLoader;
import com.automannn.codeGenerator.core.runtime.RuntimeInstance;
import com.automannn.codeGenerator.core.template.Template;

import java.io.StringWriter;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.Date;

/**
 * @author chenkh
 * @time 2020/12/8 21:43
 */
public class Main {
    public static void main(String[] args) throws Exception {
//        Velocity.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName());
//        Velocity.setProperty(RuntimeInstance.RESOURCE_LOADER,"classpath");
//        Velocity.init();
//
//        VelocityContext context = new VelocityContext();
//        context.put("name","automannn");
//        context.put("date", LocalDate.now().format(DateTimeFormatter.BASIC_ISO_DATE));
//
//        StringWriter writer = new StringWriter();
//        Template template = Velocity.getTemplate("myTemplate.vm");
//
//        template.merge(context,writer);
//        System.out.println(writer.toString());

        VelocityEngine engine = new VelocityEngine();
        /*xxx: 设置引擎的一些基本信息(配置引擎)*/
//        engine.setProperty("resource.loader","classpath");
//        ResourceLoader resourceLoader = new ClasspathResourceLoader();
//        engine.setProperty("classpath.resource.loader.instance", resourceLoader);
//        engine.setProperty("classpath.resource.loader.path", new File("").getAbsolutePath());

        engine.setProperty(RuntimeInstance.RESOURCE_LOADER,"classpath");
        engine.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName());

        /*xxx: 初始化引擎*/
        engine.init();

        /*xxx: 获取模板， 根据vm文件，生成的模板类*/
        Template template = engine.getTemplate("myTemplate.vm", "utf8");

        /*xxx: velocityContext，设置Velocity模板读取的内容,可以存入任意类型的对象或者遍历，让template来读取*/
        VelocityContext velocityContext = new VelocityContext();
        velocityContext.put("name","automannn");
        velocityContext.put("date",new Date().toString());
        velocityContext.put("list", Arrays.asList("alice","bob","sam"));

        StringWriter stringWriter = new StringWriter();
        template.merge(velocityContext,stringWriter);

        System.out.println(stringWriter.toString());
    }
}
